// pages/result/result.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    today:0,
    all: 0,
    allg: 0,
    todayg: 0,
    num:1,  //第几张海报
    count:0,
    all_days:0,
    ka1: false,
    ka2: false,
    ka3: false,
    ka4: false,
    ka5: false,

  },
  onShareAppMessage: function (res) {
    return {
      title: '赏金达人',
      path: '/pages/form/form_table/form_table',
      imageUrl: 'https://zs.cdbbox.com/uploads/images/20200323194452.jpg',
    }
  },
  stepGet: function () {
    var unionId = app.globalData.userInfo.unionId;
    var that = this
    //请求排行榜
    wx.request({
      url: 'https://zs.cdbbox.com/index/api/get_user_step',
      data: {
        unionid: unionId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        that.setData({
          today: res.data.today ? res.data.today:0,
          all: res.data.all ? res.data.all:0,
          todayg: Math.floor(res.data.today/1500),
          allg: Math.floor(res.data.all/1500),
        })
      }
    })
  },

  clockGet: function () {
    var unionId = app.globalData.userInfo.unionId;
    var that = this
    //请求打卡数据
    wx.request({
      url: 'https://zs.cdbbox.com/index/api/get_clock',
      data: {
        unionid: unionId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        that.setData({
          num: res.data.num ? res.data.num : 1,
          count: res.data.count ? res.data.count : 0,
          ka1: res.data.ka.ka1,
          ka2: res.data.ka.ka2,
          ka3: res.data.ka.ka3,
          ka4: res.data.ka.ka4,
          ka5: res.data.ka.ka5,
          all_days: res.data.all_days,
        })
      }
    })
  },

 onLogin:function(){
   var unionId = app.globalData.userInfo.unionId
   if (!unionId){
      wx.login({
        success: res => {
          if (res.code) {
            wx.getUserInfo({ //得到rawData, signatrue, encryptData
              success: function (data) {
                var rawData = data.rawData;
                var signature = data.signature;
                var encryptedData = data.encryptedData;
                var iv = data.iv;

                wx.request({
                  url: 'https://zs.cdbbox.com/index/api/login',
                  data: {
                    "code": res.code,
                    "rawData": rawData,
                    "signature": signature,
                    'iv': iv,
                    'encryptedData': encryptedData,
                  },
                  method: 'POST',
                  success: function (info) {
                    let userInfo = info.data
                    app.globalData.userInfo = userInfo
                    //console.log(app.globalData.userInfo)
                  }
                })
              }
            })
          }
        }
      })
    }


 },


  formClock: function () { 
    var unionId = app.globalData.userInfo.unionId
    var num = this.data.num
    console.log(num)
        //保存图片 对应
        wx.downloadFile({
          url: 'https://zs.cdbbox.com/uploads/images/' + num+'.jpg',
          success(res) {
           
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success(res1) {
                //console.log(unionId);
                wx.request({
                  url: 'https://zs.cdbbox.com/index/api/set_clock',
                  data: {
                    unionid: unionId,
                  },
                  method: 'POST',
                  success: function (res2) {
                    if (res2) {
                      wx.showToast({
                        title: '打卡成功！',
                        icon: 'success',
                        duration: 2500
                      })

                      setTimeout(function () {
                        wx.redirectTo({
                          url: "/pages/index/result/result"
                        })
                      }, 3000)
                    } else {
                      wx.showToast({
                        title: '打卡失败！',
                        icon: 'success',
                        duration: 2000
                      })
                      

                    }
                  },fail:res2=>{
                    wx.showToast({
                      title: '打卡失败，请稍候重试！',
                      icon: 'success',
                      duration: 2000
                    })
                  }
                })
              }, fail: res1=>{
                wx.showToast({
                  title: '请保存打卡图片再分享朋友圈！',
                  icon: 'success',
                  duration: 2000
                })
              }
            })
          }

    })

    


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onLogin()
    this.stepGet()
    this.clockGet()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})